/**
 * @author:cavion(曹仁道)
 * @email:caorendao187@163.com
 * 2016年8月31日 
 */
package org.rk.core.pubServer.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.rk.core.common.bean.JsonMsg;
import org.rk.core.common.constant.RkConst;
import org.rk.core.common.exception.ServiceLogicalException;
import org.rk.core.common.util.RKWebUtil;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

/**
 * @author:cavion(曹仁道)
 * @描述： 2016年8月31日 下午11:06:32
 */
public class CoreController<T> {
	public ApplicationEventPublisher publisher;

	public static final String defaultAuthError = "权限认证错误";
	public static final String defaultServerError = "服务器错误";

	protected JsonMsg ajaxSucc(String msg) {
		JsonMsg jsonMsg = new JsonMsg(msg, JsonMsg.status_200);
		return jsonMsg;
	}

	protected JsonMsg ajaxError(String msg) {
		JsonMsg jsonMsg = new JsonMsg(msg, JsonMsg.status_500);
		return jsonMsg;
	}

	protected JsonMsg ajaxData(Map<String, Object> data) {
		JsonMsg jsonMsg = new JsonMsg(data, JsonMsg.status_200);
		return jsonMsg;
	}

	protected JsonMsg ajaxSuccUrl(String succUrl) {
		JsonMsg jsonMsg = new JsonMsg(succUrl, JsonMsg.status_200);
		return jsonMsg;
	}

	protected Object forward(String viewPath, String viewName) {
		if (!viewPath.startsWith("/")) {
			viewPath = "/" + viewPath;
		}
		return "forward:" + viewPath + viewName;
	}

	protected Object redirect(String viewPath, String viewName) {
		if (!viewPath.startsWith("/")) {
			viewPath = "/" + viewPath;
		}
		return "redirect:" + viewPath + viewName;
	}

	@ExceptionHandler
	@ResponseBody
	public Object processGlobalExceptions(HttpServletRequest request, HttpServletResponse httpResponse,
			HandlerMethod handlerMethod, Exception e) throws Exception {
		// request.setAttribute("exceptionMessage", e);
		if (e instanceof ServiceLogicalException) {
			if (RKWebUtil.isAjax(request)) {
				RKWebUtil.respJSON(httpResponse, ajaxError(e.getMessage()));
				return null;
			} else {
				httpResponse.setHeader("rk_header_msg", java.net.URLEncoder.encode(e.getMessage(), "UTF-8"));
				RKWebUtil.redirect(request, httpResponse, RkConst.error_page.error_page_300);
				return null;
			}
		} else if (e instanceof UnauthorizedException || e instanceof UnauthenticatedException) {// 未授权
			StringBuffer msg = new StringBuffer().append("您没有该操作权限");
			if (RKWebUtil.isAjax(request)) {
				RKWebUtil.respJSON(httpResponse, ajaxError(defaultAuthError + "," + msg));
				return null;
			} else {
				RKWebUtil.redirect(request, httpResponse, RkConst.error_page.error_page_401);
				return null;
			}
		} else if (e instanceof Exception) {
			e.printStackTrace();
			if (RKWebUtil.isAjax(request)) {
				RKWebUtil.respJSON(httpResponse, ajaxError(defaultServerError + e.getMessage()));
				return null;
			} else {
				RKWebUtil.redirect(request, httpResponse, RkConst.error_page.error_page_500);
				return null;
			}
		}
		return null;
	}
}
